// 子弹类
// 子弹类型，攻击力，类名标识
// 0普通子弹，1戴帽子火炮子弹，2火盆加持子弹
// [{type:0,hit:1.5,className:'bullt1'},{type:1,hit:2,className:'bullt2'},{type:2,hit:+=1.5,className:'bullt_fire'}]
class Bullets {
    constructor(obj){
        let s = {
            x:obj.x,
            y:obj.y,
            type:obj.type, //那种植物，用于绘制不同子弹
            id:obj.id,
        }
        Object.assign(this,s)
    }
    draw(){
        // 当僵尸出现，子弹才开始射击
        let zom = null
        let bt = setInterval(()=>{
            zom = document.querySelector('.zoms')
            if(!zom) return
            clearInterval(bt)
            // zom.forEach(item=>{
            //     if(parseFloat(this.y)>=parseFloat(item.style.top)&&parseFloat(this.y)<=parseFloat(item.style.top)+80){
                    
            //     }
            // })
            // 绘制子弹--单颗子弹
            let speed = 0;
            let sp = 2
            let app = document.querySelector('#app')
            let img = new Image()
            img.src = './images/bullet.png'
            img.className = 'bullet'
            app.appendChild(img)
            let bullet = document.querySelector('.bullet')
            bullet.style.position = 'absolute'
            bullet.style.left = this.x + 'px'
            bullet.style.top = this.y+10 + 'px'
            bullet.className = 'bullet' + this.id + ' bullets'
            
            this['buTime'+this.id] = setInterval(()=>{
                timers.push(this['buTime'+this.id])
                let zoms = document.querySelectorAll('.zoms')
                speed = speed + 2
                // 遇到火盆，更换子弹图片，强化子弹威力
                fire&&fire.forEach(item=>{
                    if(parseFloat(item.style.left) === parseFloat(bullet.style.left)&&parseFloat(item.style.top) === parseFloat(bullet.style.top)-10){
                        bullet.setAttribute('src' , 'images/bullet_2.png')
                        bullet.className = 'bullet' + this.id + ' bullets bullet_fire'
                    }
                })
                // 检测游戏边界
                if(parseFloat(bullet.style.left)>=700){
                    timers.pop()
                    clearInterval(this['buTime'+this.id])
                    bullet.remove()
                    this.draw()
                }
                // 检测各个僵尸实时位置
                zoms.forEach(item=>{
                    if(parseFloat(bullet.style.left) >= parseFloat(item.style.left)+15&&parseFloat(bullet.style.left)<parseFloat(item.style.left)+80&&parseFloat(bullet.style.top)>=parseFloat(item.style.top)&&parseFloat(bullet.style.top)<=parseFloat(item.style.top)+80){
                        clearInterval(this['buTime'+this.id])
                        timers.pop()
                        bullet.remove()
                        this.draw()
                    }
                })
                bullet.style.left = this.x +speed+ 'px'
            },10)
        },30)
    }
}